<template>
  <header class="header">
    <nav class="nav-container">
      <div class="nav-wrapper">
        <!-- Logo -->
        <div class="logo-section">
          <NuxtLink to="/" class="logo-link">
            <img class="logo-icon" src="@/assets/image/etg.png"></img>
          </NuxtLink>
        </div>

        <!-- 桌面导航菜单 -->
        <div class="desktop-nav">
          <div class="nav-item" v-for="item in navigations" @mouseenter="item.showMore = true"
            @mouseleave="item.showMore = false">
            <NuxtLink :key="item.name" :to="item.children ? '' : item.href" class="nav-link"
              active-class="nav-link-active">
              {{ item.name }}
              <ChevronDownIcon class="icon-small" v-if="item.children"></ChevronDownIcon>
              <div class="dropdown-menu" v-show="item.showMore && item.children" @mouseenter="item.showMore = true"
                @mouseleave="item.showMore = false">
                <div @click="navigateTo(item.href)" class="dropdown-item" v-for="subItem in item.children">
                  <i class="fas fa-lightbulb"></i>
                  <h4 class="title">{{ subItem.name }}</h4>
                  <p class="content">{{ subItem.desc }}</p>
                </div>
              </div>
            </NuxtLink>
          </div>
        </div>

        <!-- 移动端菜单按钮 -->
        <button @click="toggleMobileMenu" class="mobile-menu-toggle">
          <Bars3Icon v-if="!showMobileMenu" class="icon-large" />
          <XMarkIcon v-else class="icon-large" />
        </button>
      </div>

      <!-- 移动端导航菜单 -->
      <div v-show="showMobileMenu" class="mobile-nav">
        <div class="mobile-nav-content">
          <NuxtLink v-for="item in navigations" :key="item.name" :to="item.href" @click="closeMobileMenu"
            class="mobile-nav-link" active-class="mobile-nav-link-active">
            {{ item.name }}
          </NuxtLink>
        </div>
      </div>
    </nav>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Bars3Icon, XMarkIcon } from '@heroicons/vue/24/outline'
import { ChevronDownIcon } from '@heroicons/vue/24/solid'

// 响应式状态
const showMobileMenu = ref(false)

// 导航菜单
const navigations = ref([
  { name: '首页', href: '/' },
  {
    name: '产品与服务', href: '/about', children: [
      { name: "壹通关", desc: "深圳南沙海运舱单、公路舱单、香港无缝申报", href: "/" },
      { name: "壹通关务云", desc: "报关管理系统、加贸关务管理系统、保税区关务管理系统", href: "/" },
      { name: "壹通仓储云", desc: "监管仓、保税仓、特殊监管仓，仓库货物入出仓库存管理", href: "/" },
      { name: "综合服务", desc: "退关退场、进口换单", href: "/" }
    ], showMore: false
  },
  { name: '行业解决方案', href: '/products' },
  { name: '客户成功案例', href: '/news' },
  { name: '动态与资讯', href: '/contact' },
  { name: '关于我们', href: '/contact' }
]);

// 切换移动端菜单
const toggleMobileMenu = () => {
  showMobileMenu.value = !showMobileMenu.value
}

// 关闭移动端菜单
const closeMobileMenu = () => {
  showMobileMenu.value = false
}

const handleScroll = () => {
  const headerEl = document.querySelector(".header");
  if (headerEl) {
    let height = headerEl.getBoundingClientRect().height;
    if (window.pageYOffset - height > 100) {
      if (!headerEl.classList.contains("header-sticky")) {
        headerEl.classList.add("header-sticky");
      }
    } else {
      headerEl.classList.remove("header-sticky");
    }
  }
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})
</script>

<style lang="scss" scoped>
.header {
  background: transparent;
  border-bottom: 1px solid #F1F1F1;
  height: 5rem;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 2025;

  &.header-sticky {
    background: #ffffff;
    animation: dropDown 0.3s ease-in-out forwards;
    z-index: 1200;
  }
}

.nav-container {
  max-width: 80rem;
  margin: 0 auto;
  padding: 0 1rem;
}

.nav-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 5rem;
}

.logo-section {
  display: flex;
  align-items: center;
}

.logo-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.desktop-nav {
  display: none;
  align-items: center;
  gap: 2.0rem;
  height: 3.125rem;
}

.nav-item {
  height: 5rem;
  display: flex;
  align-items: center;
}

.nav-link {
  color: #374151;
  padding: 0rem 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  transition: color 0.15s ease-in-out;
  position: relative;
  line-height: 50px;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;

  &::after {
    content: '';
    position: absolute;
    width: 0;
    height: 2px;
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #0975ee;
    transition: width 0.3s ease;
  }
}

.nav-item:hover {
  .nav-link {
    background: #9FC2F2;
    border-radius: 4px;

    &::after {
      width: 70%;
    }
  }
}

@keyframes dropDown {
  from {
    transform: translateY(-80px);
  }

  to {
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.dropdown-menu {
  position: absolute;
  top: 65px;
  left: 0;
  background: #ffffff;
  border: 1px solid #F1F1F1;
  padding: 1rem;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  animation: fadeIn .3s ease-in forwards;
  border-radius: 4px;

  .dropdown-item {
    &:hover {
      border: 1px solid #F1F1F1;
      cursor: pointer;
    }

    width: 360px;
    height: 80px;
    display: grid;
    grid-template-areas: "icon title"
    ". content";
    grid-template-columns: 40px 1fr;
    grid-template-rows: repeat(2, 1fr);
    align-items: center;

    i.fas {
      grid-area: icon;
      font-size: 1rem;
      color: #0975EE;
      justify-self: center;
    }

    .title {
      grid-area: title;
      font-weight: 400;
      font-size: 1rem;
      color: #0975EE;
    }

    .content {
      grid-area: content;
      font-weight: 400;
      font-size: 12px;
      color: #888888;
    }

  }
}


.mobile-menu-toggle {
  padding: 0.5rem;
  color: #374151;
  background: none;
  border: none;
  transition: color 0.15s ease-in-out;
  cursor: pointer;
}

.mobile-menu-toggle:hover {
  color: #2563eb;
}

.mobile-menu-toggle {
  display: block;
}

.icon {
  height: 1.25rem;
  width: 1.25rem;
}

.icon-large {
  height: 1.5rem;
  width: 1.5rem;
}

.icon-small {
  height: 1rem;
  width: 1rem;
}

.mobile-nav {
  display: block;
  background-color: white;
  border-top: 1px solid #e5e7eb;
}

.mobile-nav-content {
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.mobile-nav-link {
  display: block;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  color: #374151;
  border-radius: 0.375rem;
  transition: all 0.15s ease-in-out;
}

.mobile-nav-link:hover {
  color: #2563eb;
  background-color: #f9fafb;
}

.mobile-nav-link-active {
  color: #2563eb;
  background-color: #eff6ff;
}

/* Responsive Design */
@media (min-width: 768px) {
  .nav-container {
    padding: 0 1.5rem;
  }

  .desktop-nav {
    display: flex;
  }

  .mobile-menu-toggle {
    display: none;
  }

  .mobile-nav {
    display: none;
  }
}

@media (min-width: 1024px) {
  .nav-container {
    padding: 0 2rem;
  }
}
</style>
